package com.bed.lovewithpatientbed.service.impl;

import com.bed.lovewithpatientbed.common.bean.LayUiPageBean;
import com.bed.lovewithpatientbed.dao.LogDao;
import com.bed.lovewithpatientbed.pojo.Log;
import com.bed.lovewithpatientbed.pojo.LogExample;
import com.bed.lovewithpatientbed.service.LogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Tyl
 * @version 1.0.0
 * @ClassName LogServiceImpl.java
 * @Description 日志实现类
 * @createTime 2020年10月21日 17:28:00
 */
@Service
@Transactional
public class LogServiceImpl implements LogService{
    @Resource
    LogDao logDao;
    /**
     * @description 按条件查询所有日志信息
     * @author tyl
     * @updateTime 2020/10/21 17:30
     * @param   pageBean:分页条件;operateor:操作者;result:操作结果;minTime:起始时间;maxTime:截止时间
     */
    @Override
    public List<Log> selectByExample(LayUiPageBean pageBean, String operateor, String result, String minTime, String maxTime) {
        LogExample example = new LogExample();
        LogExample.Criteria criteria = example.createCriteria();
        /*分页*/
        if(pageBean != null && pageBean.getPage() >0 && pageBean.getLimit()>0){
            example.setOffset((pageBean.getPage()-1)*pageBean.getLimit());
           example.setLimit(pageBean.getLimit().intValue());
        }
        /*添加查询条件*/
        if(operateor!=null&&!"".equals(operateor)){
            criteria.andOperateorEqualTo(operateor);
        }
        if(result!=null&&!"".equals(result)){
            criteria.andOperateresultEqualTo(result);
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date minDate = null;
        Date maxDate = null;
        /*添加日期条件*/
        if(minTime!=null&&!"".equals(minTime)&&maxTime!=null&&!"".equals(maxTime)){
            try {
                System.out.println("++++++++"+minTime);
                minDate = format.parse(minTime);
                System.out.println("+++++++"+minDate.toString());
                maxDate = format.parse(maxTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            criteria.andOperatedateBetween(minDate,maxDate);
        }
        /*倒序查看*/
        example.setOrderByClause(" id  desc ");
        List<Log> logList = logDao.selectByExample(example);
        return logList;
    }
    /**
     * @description 查询条数
     * @author tyl
     * @updateTime 2020/10/21 20:04
     * @param
     */
    @Override
    public Integer countNum(LayUiPageBean pageBean, String operateor, String result, String minTime, String maxTime) {
        LogExample example = new LogExample();
        LogExample.Criteria criteria = example.createCriteria();
        /*分页*/
        if(pageBean != null && pageBean.getPage() >0 && pageBean.getLimit()>0){
            example.setOffset((pageBean.getPage()-1)*pageBean.getLimit());
            example.setLimit(pageBean.getLimit().intValue());
        }
        /*添加查询条件*/
        if(operateor!=null&&!"".equals(operateor)){
            criteria.andOperateorEqualTo(operateor);
        }
        if(result!=null&&!"".equals(result)){
            criteria.andOperateresultEqualTo(result);
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date minDate = null;
        Date maxDate = null;
        /*添加日期条件*/
        if(minTime!=null&&!"".equals(minTime)&&maxTime!=null&&!"".equals(maxTime)){
            try {
                System.out.println("++++++++"+minTime);
                minDate = format.parse(minTime);
                System.out.println("+++++++"+minDate.toString());
                maxDate = format.parse(maxTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            criteria.andOperatedateBetween(minDate,maxDate);
        }
        Long count = logDao.countByExample(example);
        return  count.intValue();
    }


}
